package org.cangku.shiro;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.cangku.dao.UserInfoDao;
import org.springframework.beans.factory.annotation.Autowired;

public class MyRealm extends AuthorizingRealm {

    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token =(UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();

        //根据用户名获取数据库中的密码
        String pwd = userInfoDao.getPwdByName(username);
        if(pwd == null || "".equals(pwd)){
            return null;
        }
        //AuthenticationInfo info = new SimpleAuthenticationInfo(username,pwd,"myrealm");
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username,pwd, ByteSource.Util.bytes("Shiro"),"myrealm") ;
        return info;
    }
}
